package modles

import (
	"gorm.io/gorm"
)

type Problem struct {
	gorm.Model
	Identity          string             `gorm:"column:identity;type:varchar(36)" json:"identity"` //问题表1唯一标识
	ProblemCategories []*ProblemCategory `gorm:"foreignKey:problem_id;references:id"`
	Title             string             `gorm:"column:title;type:varchar(255)" json:"title"` //文章标题
	Content           string             `gorm:"column:content;type:text" json:"content"`     // 文章内容
	MaxRuntime        int                `gorm:"column:max_runtime;type:int" json:"maxRuntime"`
	MaxMem            int                `gorm:"column:max_mem;type:int" json:"maxMem"`
	TestCases         []*TestCase        `gorm:"foreignKey:problem_identity;references:identity"`
}

func (table *Problem) TableName() string {
	return "problem"
}

func GetProblemList(keywords string, categoryIdentity string) *gorm.DB {
	tx := DB.Model(new(Problem)).Preload("ProblemCategories").Preload("ProblemCategories.Category").
		Where("title LIKE ? OR content like ? ", "%"+keywords+"%", "%"+keywords+"%")
	if categoryIdentity != "" {
		tx.Joins("right join problem_category pc on pc.problem_id=problem.id").
			Where("pc.category_id=(SELECT	c.id FROM category c where c.identity=?)", categoryIdentity)
	}
	return tx
}
